package com.qqtech.qquan.vote.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.vote.dao.VoteResultDao;
import com.qqtech.qquan.vote.model.VoteResult;
import com.qqtech.qquan.vote.model.VoteResultQuery;
import com.qqtech.qquan.vote.model.VoteResultVo;

@Service
public class VoteResultServiceImpl extends BaseServiceImpl<VoteResult>
		implements VoteResultService {
	public static final Logger log = LoggerFactory.getLogger(VoteResultServiceImpl.class);
	
	@Resource
	private VoteResultDao voteResultDao;

	@Override
	protected BaseDao<VoteResult> getBaseDao() {
		return voteResultDao;
	}

	@Override
	public int getTotalVoteResult(Integer voteId) {
		VoteResult voteResult=new VoteResult();
		voteResult.setVoteId(voteId);
		return voteResultDao.getGroupByUserIdCount(voteResult);
	}

	@Override
	public int getVoteItemResult(Integer voteId, Integer voteItemId) {
		VoteResult voteResult=new VoteResult();
		voteResult.setVoteId(voteId);
		voteResult.setVoteItemId(voteItemId);
		return voteResultDao.getGroupByUserIdCount(voteResult);
	}

	@Override
	public boolean isResult(Integer voteId, Integer userId) {
		VoteResult voteResult=new VoteResult();
		voteResult.setVoteId(voteId);
		voteResult.setUserId(userId);
		int count=voteResultDao.queryCount(voteResult);
		if(count>0){
			return true;
		}
		return false;
	}
	
	@Override
	public List<VoteResultVo> myLastSomeVoteResults(Integer userId, Integer some) {
		VoteResultQuery resultQ = new VoteResultQuery();
		resultQ.setUserId(userId);
		Pageable pageKit = new PageKit(1,some, new PageOrder(Direction.DESC, "newAt"));
		return voteResultDao.queryList(resultQ, pageKit);
	}
}